<?php
namespace App\Service;
use App\Common\Utill\OperationLog;
use App\Models\Category;
use Illuminate\Support\Facades\Log;

class CategoryService extends CommonService
{
    public function __construct()
    {
        parent::__construct();
    }

     public function getListData($request,$num)
     {
          $model = new Category();
          $query = $model::query();
          //账号权限类型
          $userAuthType = self::accountAuthType();
          //网站
          $websiteId = $request->input('websiteId','');
          if($websiteId){
              $query->where('website_id',$websiteId);
          } else{
              if($userAuthType != 1){
                  $websiteArr = WebsitesAdminService::getAdminWebsites();
                  if($websiteArr){
                      $query->whereIn('website_id', $websiteArr);
                  }else{
                      $query->where('website_id', -1);
                  }
              }
          }
          //名称
          $title = $request->input('title','');
          if($title){
              $query->where('category_name','like','%'.$title.'%');
          }
          $query->where("is_del",1);
          $query->orderBy('id','desc');
          //网站信息
          $websiteData = WebsitesService::getWebsiteData(2,1);
          $result = $query->paginate($num)->toArray();
          foreach ($result['data'] as $key => $val) {
              if($val['type'] == 1){
                  $typeName = "标题";
              }else{
                  $typeName = "内容";
              }
              $result['data'][$key]['type_name'] = $typeName;
              $result['data'][$key]['website_name'] = $websiteData[$val['website_id']] ?? '';
          }
          return  $this->success('SUCCESS',$result['total'],$result['data']);
     }


    /**
     * 添加数据保存
     * @return void
     */
     public function create($data)
     {
         if(!$data['websiteId']){
             return $this->error('请选择网站');
         }
         $operationData = self::operationData($data['websiteId']);
         $whereIni['admin_id'] = $operationData['admin_id'];
         $whereIni['website_id'] = $data['websiteId'];
         $whereIni['is_del'] = 1;
         $whereIni['category_name'] = $data['category_name'];
         $isConfirm = Category::query()->where($whereIni)->exists();
         if($isConfirm){
             return $this->error('该网站关键词已存在');
         }
         $model = new Category();
         $model->admin_id = $operationData['admin_id'];
         $model->category_name = $data['category_name'] ?? '';
         $model->website_id    = $data['websiteId'] ?? 0;
         $model->type  = 1;
         $model->is_del  = 1;
         $model->operation_id  = $operationData['operation_id'];
         $model->created_at  = date("Y-m-d H:i:s");
         if(!$model->save()){
             return $this->error('添加失败！！！');
         }else{
             $operationData=['category_name'=>$model->category_name,'website_id'=>$model->website_id];
             OperationLog::record(1,'关键字管理-添加',$model->id,$operationData);
             return $this->success('添加成功');
         }
     }

    /**
     * 编辑
     * @param $data
     * @return void
     */
     public function update($data){
         $oldData = Category::query()->where('id',$data['id'])->first()->toArray();
         $model = Category::findOrFail($data['id']);
         if($model->website_id != $data['websiteId']){
             $operationData = self::operationData($data['websiteId']);
             $model->admin_id = $operationData['admin_id'];
             $model->operation_id = $operationData['operation_id'];
         }
         $model->category_name = $data['category_name'] ?? '';
         $model->website_id = $data['websiteId'] ?? '';
         $model->updated_at  = date("Y-m-d H:i:s");
         if(!$model->save()){
             return $this->error('修改失败');
         }
         $operationData=['category_name'=>$model->category_name,'website_id'=>$model->website_id];
         OperationLog::record(2,'关键字管理-编辑',$data['id'],$operationData,$oldData);
         return $this->success("修改成功");
     }

    /**
     * 释放
     * @param $id
     * @return void
     */
     public function release($id)
     {
         $result = Category::findOrFail($id);
         $result->admin_id =  0;
         $result->website_id =  0;
         $result->operation_id = $this->adminId;
         $result->updated_at = date("Y-m-d H:i:s");
         if (!$result->save()) {
             return $this->error('操作失败');
         }
         OperationLog::record(6,'关键字管理-释放',$id);
         return $this->success("操作成功");
     }

    /**
     * 单个删除
     */
    public function delOne($id){
        $result = Category::findOrFail($id);
        $result->is_del = -1;
        $result->deleted_at = date("Y-m-d H:i:s");
        if (!$result->save()) {
            return $this->error('删除失败');
        }
        OperationLog::record(3,'关键字管理-删除',$id);
        return $this->success("删除成功");
    }

    /**
     * 批量删除
     * @return void
     */
    public function delAll($idArr)
    {
        if(!$idArr){
            return $this->error('删除内容不能为空');
        }
        Log::info("删除id:",$idArr);
        $delData['is_del'] = -1;
        $delData['deleted_at'] = date("Y-m-d H:i:s");
        $result = Category::whereIn("id",$idArr)->update($delData);
        if(!$result){
            return $this->error('删除失败');
        }
        OperationLog::record(3,'关键字管理-删除',0,'','',implode(",",$idArr));
        return $this->success("删除成功");
    }


}
